package com.bw.zzy.partal2.service.impl;
import cn.hutool.core.map.MapUtil;
import com.bw.zzy.partal2.db.dao.MedicalDeptDao;
import com.bw.zzy.partal2.exception.GlobalException;
import com.bw.zzy.partal2.pojo.MedicalDept;
import com.bw.zzy.partal2.service.MedicalDeptService;
import com.bw.zzy.partal2.utils.PageUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;


@Service
@Slf4j
public class MedicalDeptServiceImpl implements MedicalDeptService {

    @Autowired
    private MedicalDeptDao medicalDeptDao;

    @Override
    public HashMap selectDeptAndSub() {
        ArrayList<HashMap> list = medicalDeptDao.selectDeptAndSub();
        LinkedHashMap<String, ArrayList> map = new LinkedHashMap<String, ArrayList>();
        // 遍历部门和子部门信息列表
        for (HashMap<String, Object> instance : list) {
            Integer subId = (Integer) instance.get("subId"); // 获取子部门ID
            String deptName = (String) instance.get("deptName"); // 获取部门名称
            String subName = (String) instance.get("subName"); // 获取子部门名称
            // 将子部门信息添加到对应的部门列表中
            /*
             * computeIfAbsent：
             *   如果 Map 中存在指定的键，那么不会执行任何操作，直接返回该键对应的值。
             * */
            map.computeIfAbsent(deptName, k -> new ArrayList<>())   /*map中有和deptName对应的值，返回，*/
                    .add(new HashMap<>() {{                      /*    //没有就新建一个ArrayList绑定信息*/
                        put("subId", subId);
                        put("subName", subName);
                    }});
        }
        return map;
    }

    @Override
    public ArrayList<HashMap> selectAll() {

        return medicalDeptDao.selectAll();
    }

    @Override
    public PageUtils selectConditionByPage(Map param) {

        ArrayList<HashMap> list = null;
        // 查询有多少个科室
        long count = medicalDeptDao.selectConditionByPageCount(param);
        if (count > 0) {
            list = medicalDeptDao.selectConditionByPage(param);
        } else {
            list = new ArrayList<>();
        }
        int page = MapUtil.getInt(param, "page");
        int length = MapUtil.getInt(param, "length");
        PageUtils pageUtils = new PageUtils(list, count, page, length);
        return pageUtils;
    }

    @Override
    @Transactional
    public void insert(MedicalDept medicalDept) {
        medicalDeptDao.insert(medicalDept);

    }

    @Override
    public HashMap selectById(Integer id) {
       HashMap map =  medicalDeptDao.selectById(id);
        return map;
    }

    @Transactional
    @Override
    public void update(MedicalDept entity) {
        medicalDeptDao.update(entity);
    }

    @Transactional
    @Override
    public void deleteByIds(Integer[] ids) {
        long count = medicalDeptDao.selectSubCountByIds(ids);

        if (count == 0) {
            medicalDeptDao.deleteByIds(ids);
        } else {
            throw new GlobalException("科室存在关联诊室，无法删除记录");
        }
    }

}
